package com.clip.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.clip.action.util.SessionAttributeKey;
import com.clip.entity.ClipEvent;
import com.clip.entity.Clipmessage;
import com.clip.entity.MsgNotice;
import com.clip.entity.SystemMessage;
import com.clip.entity.User;
import com.clip.service.ContactService;
import com.clip.service.EventService;
import com.clip.service.MessageService;
import com.clip.service.UserService;
import com.clip.util.Struts2Utils;
import com.clip.util.ToolsUtils;
import com.clip.util.json.JSONArray;
import com.clip.util.json.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class MessageAction extends ActionSupport{
	
	@Autowired
	private MessageService messageService;
	@Autowired
	private UserService userService;
	@Autowired
	private EventService eventService;
	@Autowired
	private ContactService contactService;
	
	private static final long serialVersionUID = 6890837656426055604L;
	
	private String type;

	private int noticeid;
	private int systemmsgid;
	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	/**
	 * list request		->User
	 * 		systemmsg	->SystemMessage
	 * 		comment		->Clipmessage
	 * 		remind		->ClipEvent
	 * 			
	 */
	public void loadMessage(){
		HttpServletRequest request=ServletActionContext.getRequest();
		User currentuser=(User) request.getSession().getAttribute(SessionAttributeKey.USER);
		List list = messageService.findMessage(type,currentuser);
		JSONObject returnObj=new JSONObject();
		JSONArray array=new JSONArray();
		if(type.equals("request")){
			if(currentuser.getUsertype()==2){
				returnObj.put("requests",array);
			}else{
				for (Object object : list) {
					User tmp=(User) object;
					JSONObject tempobj=new JSONObject();
					tempobj.put("contactname", tmp.getName());
					tempobj.put("sex", tmp.getSex());
					tempobj.put("qq", tmp.getQq()!=null&&tmp.getQq().length()>0?tmp.getQq():"");
					tempobj.put("contactemail", tmp.getEmail());
					tempobj.put("contactuid", tmp.getId());
					List templist=this.userService.getUserLogoByUid(tmp.getId());
					tempobj.put("smalllogo",templist.get(1));
					tempobj.put("biglogo", templist.get(0));
					tempobj.put("birth", tmp.getBirth());
					array.add(tempobj);
				}
				returnObj.put("requests", array);
			}
		}else if(type.equals("systemmsg")){
			//更新档期通知的状态systemMessage
			for (Object object : list) {
				SystemMessage systemMessage=(SystemMessage) object;
				if(systemMessage.getCheckstatus()==0){
					systemMessage.setCheckstatus(1);
					this.messageService.updateSystemMessage(systemMessage);
				}
				String fromuname=this.contactService.getUserName(currentuser.getId(), systemMessage.getFromuid());
				if(fromuname==null){
					this.messageService.deleteSystemMessage(systemMessage);
					continue;
				}
				JSONObject temp=new JSONObject();
				temp.put("msgid", systemMessage.getId());
				int eventid=systemMessage.getEventid();
				ClipEvent clipEvent=this.eventService.getDetailClipEvent(eventid);
				if(clipEvent==null){
					this.messageService.deleteSystemMessage(systemMessage);
					continue;
				}
				String title=clipEvent.getTitle();
				if(title==null||title.length()==0){
					temp.put("eventid", 0);
				}else{
					temp.put("eventid", eventid);
					temp.put("eventtitle", title);
				}
				temp.put("msgtype", systemMessage.getMsgtype());
				temp.put("msgtime", systemMessage.getMsgtime());
				temp.put("fromuid", systemMessage.getFromuid());
				temp.put("fromuname", this.contactService.getUserName(currentuser.getId(), systemMessage.getFromuid()));
				List userlogo=this.userService.getUserLogoByUid(systemMessage.getFromuid());
				temp.put("frombiglogo", userlogo.get(0));
				temp.put("fromsmalllogo", userlogo.get(1));
				temp.put("shareid",clipEvent.getShareid());
				array.add(temp);
			}
			returnObj.put("systemmsgs",array);
		}else if(type.equals("comment")){
			//更新评论的状态msgNotice
			for (Object object : list) {
				JSONObject temp=new JSONObject();
				MsgNotice msgNotice=(MsgNotice) object;
				if(msgNotice.getCheckstatus()==0){
					msgNotice.setCheckstatus(1);
					this.messageService.updateMsgNotice(msgNotice);
				}
				ClipEvent clipEvent=eventService.getClipEventById(msgNotice.getEventid());
				if(clipEvent==null){
					this.messageService.deleteMsgNotice(msgNotice);
					continue;
				}
				Clipmessage clipmessage=messageService.findBymessageid(msgNotice.getMsgid());
				if(clipmessage==null){
					this.messageService.deleteMsgNotice(msgNotice);
					continue;
				}
				int commentuid=clipmessage.getUid();
				String name=this.contactService.getUserName(currentuser.getId(), commentuid);
				if(name==null){
					this.messageService.deleteMsgNotice(msgNotice);
					continue;
				}
				temp.put("noticeid", msgNotice.getId());
				temp.put("msgid", clipmessage.getId());
//				temp.put("", value)
				temp.put("eventid",msgNotice.getEventid());
				temp.put("title",clipEvent.getTitle());
				temp.put("uid",commentuid);
				temp.put("name",name);
				List templist=this.userService.getUserLogoByUid(commentuid);
				temp.put("biglogo", templist.get(0));
				temp.put("smalllogo", templist.get(1));
				temp.put("commentcontent", clipmessage.getContent());
				temp.put("commenttype", clipmessage.getMsgtype());
				temp.put("commenttime", clipmessage.getReplytime());
				temp.put("shareid", clipEvent.getShareid());
				array.add(temp);
			}
			returnObj.put("comments",array);
		}else if(type.equals("remind")){
			for (Object object : list) {
				ClipEvent clipEvent=(ClipEvent) object;
				int eventid=clipEvent.getId();
				String title=clipEvent.getTitle();
				long clocktime=clipEvent.getAlerttime();
				JSONObject temp=new JSONObject();
				temp.put("eventid", eventid);
				temp.put("title", title);
				temp.put("starttime", clipEvent.getBegintime());
				temp.put("endtime", clipEvent.getEndtime());
				temp.put("iswholeday", clipEvent.getIswholeday());
				temp.put("clocktime", clocktime);
				array.add(temp);
			}
			returnObj.put("alertevents",array);
		}
		
		Struts2Utils.renderJson(returnObj.toJSONString());
	}
	
	public void deleteSystemMessage(){
		HttpServletRequest request=ServletActionContext.getRequest();
		User currentUser=(User) request.getSession().getAttribute(SessionAttributeKey.USER);
		JSONObject returnObj=new JSONObject();
		boolean issuccess=this.messageService.deleteSystemMessage(currentUser.getId(),systemmsgid);
		if(issuccess){
			returnObj.put("result", 1);
		}else{
			returnObj.put("result", 0);
			returnObj.put("errormsg","通知无法删除");
		}
		Struts2Utils.renderJson(returnObj.toJSONString());
	}
	
	public void deleteMsgNotice(){
		HttpServletRequest request=ServletActionContext.getRequest();
		User currentUser=(User) request.getSession().getAttribute(SessionAttributeKey.USER);
		JSONObject returnObj=new JSONObject();
		boolean result=this.messageService.deleteMsgNotice(currentUser.getId(),noticeid);
		if(result){
			returnObj.put("result", 1);
		}else{
			returnObj.put("result", 0);
			returnObj.put("errormsg", "评论无法忽略");
		}
		Struts2Utils.renderJson(returnObj.toJSONString());
	}
	
	public void getMsgCount(){
		HttpServletRequest request=ServletActionContext.getRequest();
		User user=(User) request.getSession().getAttribute(SessionAttributeKey.USER);
		if(user!=null){
			Map<String,Object> map = messageService.findMessageCount(user);
			Struts2Utils.renderXml(ToolsUtils.getDOM(map));
		}else
			Struts2Utils.renderHtml("");
	}
	public void ignoreNotice(){
		this.messageService.ignoreMessage(this.noticeid);
		Struts2Utils.renderText("{result:1}");
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getNoticeid() {
		return noticeid;
	}

	public void setNoticeid(int noticeid) {
		this.noticeid = noticeid;
	}
	public int getSystemmsgid() {
		return systemmsgid;
	}
	public void setSystemmsgid(int systemmsgid) {
		this.systemmsgid = systemmsgid;
	}
	
	
}
